﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application,
        activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        var pck = Windows.ApplicationModel.Package.current;

        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {

            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll().done(function processComplete(e) {
                // Clear log for samples
                Eight.Log.clear().done();

                // Setup Settings charm example
                var areas = [
                    new Eight.Settings.Area("help", "Help", "/pages/settings/help.html")
                ];
                Eight.Settings.initialize(areas);

                // Setup Binding example
                BindingSample.init();

                // Initialize Unit Tests
                QUnitMetro.autoLoadTests = false;
                Tests.startTests();
            }));
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.onerror = function (eventInfo) {
        var error = eventInfo.detail,
            errorMessage = Eight.String.format("{0} - Error in {1} at Line: {2}, Character: {3}.\n\r {4}\n\r", 
                new Date().toString(), error.errorUrl, error.errorLine, error.errorCharacter, error.errorMessage);

        Eight.Log.log(errorMessage).done(function (e) {
            // completed ok
        },
        function (e) {
            debugger;
        });
        return true;
    };

    window.onerror = function (e) {
        return true;
    };

    app.start();
})();
